import 'package:async/async.dart';
import 'package:boss/main.dart';
import 'package:boss/models/staff.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:boss/ui/widget/radioList/model/radio_item_data.dart';
import 'package:boss/ui/widget/radioList/radio_list.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';

Future<List<Staff>> showPickerStaff(
  BuildContext context, {
  List<int> defaultSelectedId,
  bool isMultiple = true,
  List<Staff> staffs,
}) {
  return Navigator.push<List<Staff>>(
      context,
      MaterialPageRoute(
        builder: (context) => PickerStaff(defaultSelectedId, isMultiple, staffs),
      ));
}

class PickerStaff extends StatefulWidget {
  final List<int> defaultSelectedId;
  final isMultiple;
  final List<Staff> staffs;

  const PickerStaff(
    this.defaultSelectedId,
    this.isMultiple,
    this.staffs,
  );

  @override
  _PickerStaffState createState() => _PickerStaffState();
}

class _PickerStaffState extends State<PickerStaff> {
  List<RadioItemData<Staff>> staffs = [];
  List<Staff> selected = [];

  String get selectBtnText => isSelectedAll ? '取消' : '全选';

  bool get isSelectedAll => selected.length == staffs.length;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(
        title: '选择员工',
        actions: <Widget>[
          LabelButton(
            title: selectBtnText,
            onTap: () {
              selected = isSelectedAll ? [] : staffs.map((item) => item.value).toList();
              setState(() {});
            },
          ),
        ],
      ),
      body: Loader(
        loadTask: loadTask,
        builder: (context) => Column(
          children: <Widget>[
            Expanded(
              child: RadioList<Staff>(
                items: staffs,
                value: selected,
                isMultiple: widget.isMultiple,
                padding: EdgeInsets.symmetric(
                  horizontal: 25.w,
                ),
                onChanged: (value) {
                  selected = value;
                  setState(() {});
                },
              ),
            ),
            FixedBottomBtn(
              title: '保存',
              onTap: () {
                Navigator.pop(context, selected);
              },
            ),
          ],
        ),
      ),
    );
  }

  Future<Result> loadTask() async {
    List<Staff> data;

    if (widget.staffs != null) {
      data = widget.staffs;
    } else {
      try {
        var res = await api.queryAllStaffList();

        if (!res.successful()) {
          return Result.error(res.msg);
        }
        data = res.data;
      } catch (e) {
        return Result.error(e.toString());
      }
    }

    for (var staff in data) {
      staffs.add(RadioItemData(
        title: staff.realname,
        value: staff,
      ));

      if (widget.defaultSelectedId?.contains(staff.userId) ?? false) {
        selected.add(staff);
      }
    }

    return Result.value(null);
  }
}
